﻿using System.Collections;
using System.Collections.Generic;
using Puerts;
using System.IO;
using UnityEngine;

namespace Puerts
{
    public class JsLoader : ILoader
    {
        public static string PathJs = Path.GetFullPath(Path.Combine(Application.dataPath, "../JavaScript/"));
        public static string PathTs = Path.Combine(Application.dataPath, "TypeScript/");

        public static string Puerts
        {
            get
            {
                string path = Path.Combine(Application.dataPath, "Puerts");
                if (Directory.Exists(path))
                    return path;
#if UNITY_EDITOR
                foreach (string guid in UnityEditor.AssetDatabase.FindAssets("PuertsDLL"))
                {
                    string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
                    assetPath = Path.GetFullPath(assetPath);
                    path = Directory.GetParent(Path.GetDirectoryName(assetPath)).FullName;
                    return path.TrimEnd(new char[] {'/', '\\'});
                }
#endif
                return path;
            }
        }
        public static string Puerts_Extens
        {
            get
            {                
                string path = Path.Combine(Application.dataPath,"TypeScript", "APuerts"); 
                if (Directory.Exists(path))
                    return path;
#if UNITY_EDITOR
                foreach (string guid in UnityEditor.AssetDatabase.FindAssets("JsManager"))
                {
                    string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
                    assetPath = Path.GetFullPath(assetPath);
                    path = Path.GetDirectoryName(assetPath);
                    return path.TrimEnd(new char[] {'/', '\\'});
                }
#endif
                return path;
            }
        }

        private string root = "";
        private DefaultLoader _defaultLoader;

        public JsLoader()
        {
            root = PathJs;
            _defaultLoader = new DefaultLoader();
        }

        public JsLoader(string root)
        {
            this.root = root;
            _defaultLoader = new DefaultLoader();
        }

        public bool FileExists(string filepath)
        {
            if( File.Exists(Path.Combine(root, filepath)))
                return true;
            return _defaultLoader.FileExists(filepath);
        }

        public string ReadFile(string filepath, out string debugpath)
        {
            string file = Path.Combine(root, filepath);
            debugpath = filepath;
            if (File.Exists(file))
            {
#if UNITY_EDITOR
                filepath = file;
                debugpath = filepath.Replace( "\\","/");
                Debug.Log("jsLoader readFile " + filepath);
#endif
                string txt = System.IO.File.ReadAllText(filepath);
                if (!string.IsNullOrEmpty(txt))
                {
                    return txt;
                }
            }
            return _defaultLoader.ReadFile(filepath,out debugpath);
        }
    }
}